package com.qxsx.midi.activity.exam;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.qxsx.midi.R;
import com.qxsx.midi.activity.home.MusicDetailsActivity;
import com.qxsx.midi.activity.study.MidiManDetailsActivity;
import com.qxsx.midi.adapter.TeacherAdapter;
import com.qxsx.midi.config.AppContext;
import com.qxsx.midi.model.Buy;
import com.qxsx.midi.model.Teacher;
import com.qxsx.midi.utils.CustomProgress;
import com.qxsx.midi.utils.MyNet;
import com.qxsx.midi.utils.SPUtils;
import com.qxsx.midi.utils.callBackJsonArray;
import com.qxsx.midi.view.XListView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Joney on 2015/9/7.
 * MIDI先生
 */
public class MidiTeacherActivity extends Activity implements OnClickListener, XListView.IXListViewListener {
    private CustomProgress customProgress;

    //MIDI先生列表
    private XListView lv_teacher;

    private TeacherAdapter adapter;

    private List<Teacher> lists;
    private int pageNum = 1;//初始化页号为1


    private Intent intent;

    private LinearLayout ll_pop, ll_context;
    //返回按钮
    private LinearLayout Ll_back;
    //筛选
    private CheckBox header_right_btn;
    //执行下拉、上拉线程
    private Handler mHandler;
    /**
     * 城市选择中组件
     */
    //“不限”，“其他”，城市名，跳转
    private Button btn_city_nolimt, btn_other;
    private TextView tv_city;
    private LinearLayout ll_next;


    private EditText et_midiid;

    /**
     * 专业中的组件
     */
    //电吉他，爵士鼓，电贝司，音乐基础，不限
    private Button btn_guitar, btn_agitate, btn_bass, btn_basic, btn_major_nolimt;

    /**
     * 等级
     */
    // 初级。中级，高级，特级，不限
    private Button btn_one, btn_two, btn_three, btn_four, btn_grade_nolimt;

    /**
     * 性别
     */
    //男，女，不限
    private Button btn_man, btn_women, btn_sex_nolimt;


    String sex = "";
    String major_id = "";
    String grade_id = "";
    String city_id = "";

    private LinearLayout ll_midi;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_teacher);

        findById();


        lv_teacher.setPullLoadEnable(true);
        lv_teacher.setPullRefreshEnable(true);
        mHandler = new Handler();
        lv_teacher.setXListViewListener(this);


    }

    private void inItData() {
        lists = new ArrayList<>();

        adapter = new TeacherAdapter(MidiTeacherActivity.this, lists);
        lv_teacher.setAdapter(adapter);
        Miditeacher(1);


    }

    @Override
    protected void onResume() {
        super.onResume();
        String miditeacher_city_name = (String) SPUtils.get(AppContext.getContext(), "miditeacher_city_name", "");
        city_id = (String) SPUtils.get(AppContext.getContext(), "miditeacher_city_id", "");

        tv_city.setText(miditeacher_city_name);
        if (tv_city.getText().toString().equals("全国")) {
            btn_city_nolimt.setSelected(true);
            btn_other.setSelected(false);
        } else {
            btn_other.setSelected(true);
            btn_city_nolimt.setSelected(false);
        }
        inItData();


    }


    private void Miditeacher(int pageNum) {
        customProgress = CustomProgress.show(MidiTeacherActivity.this, "请稍后....", true, null);

        JSONObject object = new JSONObject();
        JSONArray array = new JSONArray();
        major_id = (String) SPUtils.get(AppContext.getContext(), "apply_major_id", "");

        try {
            object.put("city_id", city_id);
            object.put("midiId", et_midiid.getText().toString().trim());
            object.put("sex", sex);
            object.put("major_id", major_id);
            object.put("grade_id", grade_id);
            object.put("nowpage", pageNum + "");
            object.put("pagesize", "10");
            array.put(object);

        } catch (JSONException e) {
            e.printStackTrace();
        }
        MyNet.doPostJsonArray("605", array, new callBackJsonArray() {
            @Override
            public void run(Object code, Object message, JSONArray array) {
                Log.w("code", code + "");
                Log.w("message", message + "");
                Log.w("array", array + "");
                if (array == null) {
                    return;
                }
                try {
                    lists = new ArrayList<Teacher>();
                    for (int i = 0; i < array.length(); i++) {
                        JSONObject object1 = array.getJSONObject(i);
                        Teacher teacher = new Teacher();
                        teacher.setTeacher_highest_level(object1.getString("highest_level"));
                        teacher.setTeacher_id(object1.getString("id"));
                        teacher.setTeacher_introduce(object1.getString("mr_introduce"));
                        teacher.setTeacher_naem(object1.getString("real_name"));
                        teacher.setTeacher_city(object1.getString("name"));

                        teacher.setTeacher_head_img(object1.getString("head_img"));
                        teacher.setTeacher_major_name(object1.getString("major_name"));
                        lists.add(teacher);
                    }
                    adapter.removeAll();
                    adapter.addData(lists);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                customProgress.dismiss();
            }
        });


    }

    /**
     * 上啦加载
     */
    private void LoadMore() {
        pageNum++;
        JSONObject object = new JSONObject();
        JSONArray array = new JSONArray();
        major_id = (String) SPUtils.get(AppContext.getContext(), "apply_major_id", "");
        try {
            object.put("city_id", city_id);
            object.put("midiId", et_midiid.getText().toString().trim());
            object.put("sex", sex);
            object.put("major_id", major_id);
            object.put("grade_id", grade_id);
            object.put("nowpage", pageNum + "");
            object.put("pagesize", "10");
            array.put(object);

        } catch (JSONException e) {
            e.printStackTrace();
        }
        MyNet.doPostJsonArray("605", array, new callBackJsonArray() {
            @Override
            public void run(Object code, Object message, JSONArray array) {
                Log.w("code", code + "");
                Log.w("message", message + "");
                Log.w("array", array + "");
                if (array == null) {
                    return;
                }
                try {
                    lists = new ArrayList<Teacher>();
                    for (int i = 0; i < array.length(); i++) {
                        JSONObject object1 = array.getJSONObject(i);
                        Teacher teacher = new Teacher();
                        teacher.setTeacher_highest_level(object1.getString("highest_level"));
                        teacher.setTeacher_id(object1.getString("id"));
                        teacher.setTeacher_introduce(object1.getString("mr_introduce"));
                        teacher.setTeacher_naem(object1.getString("real_name"));
                        teacher.setTeacher_city(object1.getString("name"));
                        teacher.setTeacher_head_img(object1.getString("head_img"));
                        teacher.setTeacher_major_name(object1.getString("major_name"));
                        lists.add(teacher);
                        lv_teacher.stopLoadMore();

                    }
                    adapter.addData(lists);
                    lv_teacher.stopLoadMore();
                } catch (JSONException e) {
                    e.printStackTrace();
                }


            }
        });


    }


    /**
     * 下拉刷新
     */
    private void LoadRefresh() {
        pageNum = 1;
        JSONObject object = new JSONObject();
        JSONArray array = new JSONArray();
        major_id = (String) SPUtils.get(AppContext.getContext(), "apply_major_id", "");
        try {
            object.put("city_id", city_id);
            object.put("midiId", et_midiid.getText().toString().trim());
            object.put("sex", sex);
            object.put("major_id", major_id);
            object.put("grade_id", grade_id);
            object.put("nowpage", pageNum + "");
            object.put("pagesize", "10");
            array.put(object);

        } catch (JSONException e) {
            e.printStackTrace();
        }
        MyNet.doPostJsonArray("605", array, new callBackJsonArray() {
            @Override
            public void run(Object code, Object message, JSONArray array) {
                Log.w("code", code + "");
                Log.w("message", message + "");
                Log.w("array", array + "");
                if (array == null) {
                    return;
                }
                try {
                    lists = new ArrayList<Teacher>();
                    for (int i = 0; i < array.length(); i++) {
                        JSONObject object1 = array.getJSONObject(i);
                        Teacher teacher = new Teacher();
                        teacher.setTeacher_highest_level(object1.getString("highest_level"));
                        teacher.setTeacher_id(object1.getString("id"));
                        teacher.setTeacher_introduce(object1.getString("mr_introduce"));
                        teacher.setTeacher_naem(object1.getString("real_name"));
                        teacher.setTeacher_city(object1.getString("name"));
                        teacher.setTeacher_head_img(object1.getString("head_img"));
                        teacher.setTeacher_major_name(object1.getString("major_name"));
                        lists.add(teacher);
                    }
                    adapter.removeAll();
                    adapter.addData(lists);
                    lv_teacher.stopRefresh();
                    lv_teacher.setRefreshTime("2015-1-11");
                } catch (JSONException e) {
                    e.printStackTrace();
                }


            }
        });

    }

    /**
     * 下拉刷新
     */
    @Override
    public void onRefresh() {

        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                LoadRefresh();
            }
        }, 2000);
    }

    /**
     * 上拉加载更多
     */
    @Override
    public void onLoadMore() {
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                LoadMore();
            }
        }, 2000);

    }


    private void miditeacher() {


        JSONArray array = new JSONArray();
        JSONObject object = new JSONObject();

        try {
            object.put("article_id", "become_midi_sir");
            array.put(object);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        MyNet.doPostJsonArray("8030", array, new callBackJsonArray() {
            @Override
            public void run(Object code, Object message, JSONArray array) {
                try {
                    JSONObject object1 = array.getJSONObject(0);
                    String url = object1.getString("url");

                    Intent intent = new Intent(MidiTeacherActivity.this, ItMidiActivity.class);
                    intent.putExtra("url", url);
                    startActivity(intent);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });


    }


    private void findById() {
        ll_pop = (LinearLayout) findViewById(R.id.ll_pop);
        ll_pop.setFocusable(true);
        ll_pop.setClickable(true);


        //如何成为MIDI先生
        ll_midi = (LinearLayout) findViewById(R.id.ll_midi);
        ll_midi.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                miditeacher();
            }
        });

        //城市
        btn_city_nolimt = (Button) findViewById(R.id.btn_city_nolimt);
        btn_other = (Button) findViewById(R.id.btn_other);
        ll_next = (LinearLayout) findViewById(R.id.ll_next);

        ll_next.setOnClickListener(this);
        btn_city_nolimt.setOnClickListener(this);
        btn_other.setOnClickListener(this);

        //专业
        btn_guitar = (Button) findViewById(R.id.btn_guitar);
        btn_guitar.setOnClickListener(this);

        btn_agitate = (Button) findViewById(R.id.btn_agitate);
        btn_agitate.setOnClickListener(this);

        btn_bass = (Button) findViewById(R.id.btn_bass);
        btn_bass.setOnClickListener(this);

        btn_basic = (Button) findViewById(R.id.btn_basic);
        btn_basic.setOnClickListener(this);

        btn_major_nolimt = (Button) findViewById(R.id.btn_major_nolimt);
        btn_major_nolimt.setOnClickListener(this);
        btn_major_nolimt.setSelected(true);

        //等级
        btn_one = (Button) findViewById(R.id.btn_one);
        btn_one.setOnClickListener(this);

        btn_two = (Button) findViewById(R.id.btn_two);
        btn_two.setOnClickListener(this);

        btn_three = (Button) findViewById(R.id.btn_three);
        btn_three.setOnClickListener(this);

        btn_four = (Button) findViewById(R.id.btn_four);
        btn_four.setOnClickListener(this);

        btn_grade_nolimt = (Button) findViewById(R.id.btn_grade_nolimt);
        btn_grade_nolimt.setOnClickListener(this);
        btn_grade_nolimt.setSelected(true);
        //性别
        btn_man = (Button) findViewById(R.id.btn_man);
        btn_man.setOnClickListener(this);

        btn_women = (Button) findViewById(R.id.btn_women);
        btn_women.setOnClickListener(this);

        btn_sex_nolimt = (Button) findViewById(R.id.btn_sex_nolimt);
        btn_sex_nolimt.setOnClickListener(this);
        btn_sex_nolimt.setSelected(true);

        et_midiid = (EditText) findViewById(R.id.et_midiid);
        et_midiid.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
        et_midiid.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH || (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
                    Miditeacher(1);
                    //Toast.makeText(StudyClubSearchActivity.this, "点击了搜索", Toast.LENGTH_SHORT).show();
                    return true;
                }
                return false;
            }
        });


        tv_city = (TextView) findViewById(R.id.tv_city);


        lv_teacher = (XListView) findViewById(R.id.lv_teacher);
        ll_context = (LinearLayout) findViewById(R.id.ll_context);
        Ll_back = (LinearLayout) findViewById(R.id.header_left_ll);
        Ll_back.setOnClickListener(this);
        header_right_btn = (CheckBox) findViewById(R.id.header_right_btn);
        header_right_btn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    ll_pop.setVisibility(View.VISIBLE);
                    lv_teacher.setFocusable(false);
                    ll_pop.setFocusable(true);
                } else {
                    inItData();
                    ll_pop.setVisibility(View.GONE);
                    lv_teacher.setFocusable(true);
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.header_left_ll:
                finish();
                break;
            case R.id.ll_next:
                startActivity(new Intent(MidiTeacherActivity.this, MidiTeacherCityListActivity.class));
                break;
            //城市 Button btn_city_nolimt, btn_other;
            case R.id.btn_city_nolimt:
                SPUtils.put(AppContext.getContext(), "miditeacher_city_id", "");
                SPUtils.put(AppContext.getContext(), "miditeacher_city_name", "全国");
                tv_city.setText("全国");
                city_id = "";
                btn_city_nolimt.setSelected(true);
                btn_other.setSelected(false);
                break;
            case R.id.btn_other:
                btn_city_nolimt.setSelected(false);
                btn_other.setSelected(true);
                startActivity(new Intent(MidiTeacherActivity.this, MidiTeacherCityListActivity.class));
                break;
            //专业Button btn_guitar, btn_agitate, btn_bass, btn_basic, btn_major_nolimt;
            case R.id.btn_guitar:
                major_id = "2172405d-17bb-5f40-da92-cf9cc285a980";
                SPUtils.put(AppContext.getContext(), "apply_major_id", major_id);
                btn_guitar.setSelected(true);
                btn_agitate.setSelected(false);
                btn_bass.setSelected(false);
                btn_basic.setSelected(false);
                btn_major_nolimt.setSelected(false);
                break;
            case R.id.btn_agitate:
                major_id = "cd12a552-95ee-9d9b-1efb-8c302c6b52c2";
                SPUtils.put(AppContext.getContext(), "apply_major_id", major_id);
                btn_guitar.setSelected(false);
                btn_agitate.setSelected(true);
                btn_bass.setSelected(false);
                btn_basic.setSelected(false);
                btn_major_nolimt.setSelected(false);
                break;
            case R.id.btn_bass:
                major_id = "5bb9bcb5-88a0-9c85-0a15-39b51c1142be";
                SPUtils.put(AppContext.getContext(), "apply_major_id", major_id);
                btn_guitar.setSelected(false);
                btn_agitate.setSelected(false);
                btn_bass.setSelected(true);
                btn_basic.setSelected(false);
                btn_major_nolimt.setSelected(false);
                break;
            case R.id.btn_basic:
                major_id = "87b65e6a-72fc-4617-d61b-351a7d080ed7";
                SPUtils.put(AppContext.getContext(), "apply_major_id", major_id);
                btn_guitar.setSelected(false);
                btn_agitate.setSelected(false);
                btn_bass.setSelected(false);
                btn_basic.setSelected(true);
                btn_major_nolimt.setSelected(false);
                break;
            case R.id.btn_major_nolimt:
                major_id = "";
                SPUtils.put(AppContext.getContext(), "apply_major_id", major_id);
                btn_guitar.setSelected(false);
                btn_agitate.setSelected(false);
                btn_bass.setSelected(false);
                btn_basic.setSelected(false);
                btn_major_nolimt.setSelected(true);
                break;

            //等级Button btn_one, btn_two, btn_three, btn_four, btn_grade_nolimt;
            case R.id.btn_one:
                grade_id = "6";
                btn_one.setSelected(true);
                btn_two.setSelected(false);
                btn_three.setSelected(false);
                btn_four.setSelected(false);
                btn_grade_nolimt.setSelected(false);
                break;
            case R.id.btn_two:
                grade_id = "7";
                btn_one.setSelected(false);
                btn_two.setSelected(true);
                btn_three.setSelected(false);
                btn_four.setSelected(false);
                btn_grade_nolimt.setSelected(false);
                break;
            case R.id.btn_three:
                grade_id = "8";
                btn_one.setSelected(false);
                btn_two.setSelected(false);
                btn_three.setSelected(true);
                btn_four.setSelected(false);
                btn_grade_nolimt.setSelected(false);
                break;
            case R.id.btn_four:
                grade_id = "9";
                btn_one.setSelected(false);
                btn_two.setSelected(false);
                btn_three.setSelected(false);
                btn_four.setSelected(true);
                btn_grade_nolimt.setSelected(false);
                break;
            case R.id.btn_grade_nolimt:
                grade_id = "";
                btn_one.setSelected(false);
                btn_two.setSelected(false);
                btn_three.setSelected(false);
                btn_four.setSelected(false);
                btn_grade_nolimt.setSelected(true);
                break;
            //性别Button btn_man, btn_women, btn_sex_nolimt;
            case R.id.btn_man:
                sex = "1";
                btn_man.setSelected(true);
                btn_women.setSelected(false);
                btn_sex_nolimt.setSelected(false);
                break;
            case R.id.btn_women:
                sex = "2";
                btn_man.setSelected(false);
                btn_women.setSelected(true);
                btn_sex_nolimt.setSelected(false);
                break;
            case R.id.btn_sex_nolimt:
                sex = "";
                btn_man.setSelected(false);
                btn_women.setSelected(false);
                btn_sex_nolimt.setSelected(true);
                break;

        }

    }

}
